package com.ca.zookeeper.base;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Author Name: Justice
 * Project Name: zookeeper
 * File Name: ZookeeperWatch1
 * Package Name: com.ca.zookeeper.base
 * Date: 2020-02-21 21:05
 * Copyright (c) 2020,All Rights Reserved.
 */


public class ZookeeperWatch1 implements Watcher {
    // JUC中用于阻塞线程的,可以在多个线程中进行阻塞
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static ZooKeeper zooKeeper;

    @Override
    public void process(WatchedEvent event) {
        System.out.println("监听事件开始执行");
        // 当连接成功后会执行当前方法, 在此处我们放行即可
        // 事件类型
        Event.EventType type = event.getType();
        // 事件状态
        Event.KeeperState state = event.getState();
        // 连接成功执行
        if (state == Event.KeeperState.SyncConnected) {
            String path1 = event.getPath();
            switch (type) {
                case None:
                    // NONE代表的是连接操作
                    System.out.println("建立连接成功");
                    // 放行
                    countDownLatch.countDown();
                    break;
                case NodeCreated:
                    String path = path1;
                    System.out.println(path + "节点创建成功");
                    break;
                case NodeDeleted:
                    System.out.println(path1 + "节点删除成功");
                    break;
                case NodeDataChanged:
                    System.out.println(path1);
                    // 监听只有一次有效, 需要继续监听, 需要重新监听
                    byte[] data = new byte[0];
                    try {
                        data = zooKeeper.getData(path1, true, null);
                        System.out.println(path1 + "节点内容发生变化:----->" + new String(data));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case NodeChildrenChanged:
                    // path1 代表哪个父目录下的子结点点发生变化
                    System.out.println(path1 + "下的子节点发生变化了");
                    break;
            }
        }
    }

    // 创建结点
    public void createNode(String path) throws KeeperException, InterruptedException {
        // 判断节点存在不存在, 并使用当前zookeeper中的watcher来进行监听
        zooKeeper.exists(path, true);
        zooKeeper.create(path, "测试创建监听".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    // 删除结点
    public void deleteNode(String path) throws KeeperException, InterruptedException {
        // 判断节点存在不存在, 并使用当前zookeeper中的watcher来进行监听
        zooKeeper.exists(path, true);
        zooKeeper.delete(path, -1);
    }

    // 更新结点
    public void setData(String path) throws KeeperException, InterruptedException {
        //如果想监听内容变化, 必须要先获取一次内容, 并要求监听
        zooKeeper.getData(path, true, null);
        zooKeeper.setData(path, "新的监听内容为111".getBytes(), -1);
    }

    // 创建子结点
    public void createChildren(String path) throws KeeperException, InterruptedException {
        // 我们要想监听某个父节点的子节点的变化, 必须先获取这个父节点的子节点
        zooKeeper.getChildren("/online03", true);
        zooKeeper.create("/online03/" + path, "information".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZookeeperWatch1 watch1 = new ZookeeperWatch1();
        zooKeeper = new ZooKeeper(AddressUtil.ADDRESS, 5000, watch1);
        countDownLatch.await();
        watch1.createNode("/bread");
        // watch1.deleteNode("/bread");
        // watch1.setData("/online03/20200108");
        // watch1.createChildren("sublime");
        System.in.read();
    }
}
